"""
    Author:
    Date: 2019-01-05

    版本管理类
"""
import os

from django.conf import settings

from utils import db

IOS_PLIST_BEGIN = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>items</key>
    <array>
        <dict>
            <key>assets</key>
            <array>
                <dict>
                    <key>kind</key>
                    <string>software-package</string>
                    <key>url</key>
                    <string>'''

IOS_PLIST_END = '''</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>full-size-image</string>
                    <key>needs-shine</key>
                    <false/>
                    <key>url</key>
                    <string>http://misc.app.res.mqnetswork.com/app_icon_yuejian.png</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>display-image</string>
                    <key>needs-shine</key>
                    <false/>
                    <key>url</key>
                    <string>http://misc.app.res.mqnetswork.com/app_icon_yuejian.png</string>
                </dict>
            </array>
            <key>metadata</key>
            <dict>
                <key>bundle-identifier</key>
                <string>com.yuanquanjiaoyou.ios2019</string>
                <key>bundle-version</key>
                <string>1.0</string>
                <key>kind</key>
                <string>software</string>
                <key>subtitle</key>
                <string></string>
                <key>title</key>
                <string>约见</string>
            </dict>
        </dict>
    </array>
</dict>
</plist>'''


def get_version_config(ios_channel, android_channel):
    """ 获取当前可下载的版本配置信息 """
    print(f'get version config: ios={ios_channel}, android={android_channel}')
    config = {}
    ios_version = get_release_version_by_channel(ios_channel)
    if ios_version is not None:
        bundle_id = ios_version['bundle_id']
        app = get_app_info(bundle_id)
        config['ios_mobile_provision'] = app['ios_provision'] if app is not None else ''
        config['ios_download_url'] = ios_version['download_url']
        # config['ios_mobile_provision'] = ios_version['ios_provision']
        # config['ios_download_url'] = "itms-services://?action=download-manifest&url=" + ios_version['plist_url']
    else:
        config['ios_mobile_provision'] = ''
        config['ios_download_url'] = ''

    android_version = get_release_version_by_channel(android_channel)
    config['android_download_url'] = android_version['download_url'] if android_version is not None else ''

    return config


def get_version_list(platform):
    """ 获取版本列表 """
    sql = '''
        SELECT      *
        FROM        cmf_app_versions
        WHERE       platform like "%%%s%%"
        ORDER BY    id DESC;
    ''' % platform
    data = db.query_data_from_db(sql)
    return data if data else []


def get_version_info(vid):
    """ 获取指定的版本信息 """
    sql = f'''
        SELECT      *
        FROM        cmf_app_versions
        WHERE       id={vid}
        LIMIT       1;
    '''
    data = db.query_data_from_db(sql)
    return data[0] if data else None


def get_release_version(platform):
    """ 获取指定平台的测试版本 """
    sql = '''
        SELECT      *
        FROM        cmf_app_versions
        WHERE       platform=%(platform)s AND
                    open_download=1
        ORDER BY    id DESC
        LIMIT       1;
    '''
    params = {
        'platform': platform,
    }
    data = db.query_data_from_db(sql, params)
    return data[0] if data else None


def get_release_version_by_channel(channel):
    """ 获取指定平台的测试版本 """
    sql = '''
        SELECT      *
        FROM        cmf_app_versions
        WHERE       channel=%(channel)s AND
                    open_download=1
        ORDER BY    build_no DESC
        LIMIT       1;
    '''
    params = {
        'channel': channel,
    }
    data = db.query_data_from_db(sql, params)
    return data[0] if data else None


# def get_release_version_by_id(id):
#     """ 获取指定平台的测试版本 """
#     sql = '''
#         SELECT      *
#         FROM        cmf_app_versions
#         WHERE       id=%(id)s;
#     '''
#     params = {
#         'id': id,
#     }
#     data = db.query_data_from_db(sql, params)
#     return data[0] if data else None

def get_app_info(bundle_id):
    """ 获取指定的版本信息 """
    sql = '''
        SELECT      *
        FROM        cmf_config_app
        WHERE       bundle_id=%(bundle_id)s
        LIMIT       1;
    '''
    params = {
        'bundle_id': bundle_id,
    }
    data = db.query_data_from_db(sql, params)
    return data[0] if data else None


if __name__ == '__main__':
    print('ok')
